package com.dldata.devtools30.controller.api;


import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.common.EnumUtils;
import com.dldata.devtools30.entity.DevtApiSubEntity;
import com.dldata.devtools30.entity.DevtApiTjoptEntity;
import com.dldata.devtools30.service.api.DevtApiMainEntityService;
import com.dldata.devtools30.service.api.DevtApiSubEntityService;
import com.dldata.devtools30.service.api.DevtApiTjoptEntityService;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/DevtApiTjoptEntityController")
public class DevtApiTjoptEntityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DevtApiTjoptEntityService devtApiTjoptEntityService;
    @Autowired
    private DevtApiSubEntityService devtApiSubEntityService;
    
    @RequestMapping(value = "/getDemoJsonByPid")
    @ResponseBody
    public Result getDemoJsonByPid(@RequestParam Map<String, String> paramMap){
        
        String pid=paramMap.get("pid");
        
        try {
            StringBuilder _str = new StringBuilder();
            List<DevtApiTjoptEntity> _list = new ArrayList<DevtApiTjoptEntity>();
            if (!StringUtil.isEmptyOrLength0(pid)) {
                _list = devtApiTjoptEntityService.getAllDataByIdOrDm(pid);
            }
  
            HashMap postJsonOne=new HashMap();
            for (DevtApiTjoptEntity _m : _list) {
                postJsonOne.put(_m.getArgname(),StringUtil.isEmptyOrLength0(_m.getBz())?"":_m.getBz());
            }
            
            HashMap dataHashMap=new HashMap();
            dataHashMap.put("data",postJsonOne);
            String jsonstrOne= JSON.toJSONString(dataHashMap,true);
            
            dataHashMap=new HashMap();
            List<Map> postJsonAry=new ArrayList<>();
            postJsonAry.add(postJsonOne);
            dataHashMap.put("data",postJsonAry);
            String jsonstrAry= JSON.toJSONString(dataHashMap,true);
            
//          postJsonOne
//          postJsonAry
            HashMap returnData=new HashMap();
            returnData.put("jsonstrOne",jsonstrOne);
            returnData.put("jsonstrAry",jsonstrAry);
            return Result.ok(returnData);
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
            return Result.error("系统错误，请稍候再试！");
            
        }
        
    }

    @RequestMapping(value = "/getAllByPid")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){

        String pid=paramMap.get("pid");

        try {
            StringBuilder _str = new StringBuilder();
            List<DevtApiTjoptEntity> _list = new ArrayList<DevtApiTjoptEntity>();
            if (!StringUtil.isEmptyOrLength0(pid)) {
                _list = devtApiTjoptEntityService.getAllDataByIdOrDm(pid);
            } else {
                _list = devtApiTjoptEntityService.getAllData();
            }
            List<Map> datalist=new ArrayList<>();
            for (DevtApiTjoptEntity _m : _list) {
                Map<String,Object> data=new HashMap<>();
                data.put("id",_m.getId());
                data.put("xh",_m.getXh());
                EnumUtils.ArgsType typeBycode = EnumUtils.ArgsType.getArgsTypeBycode(_m.getLx());
                data.put("lxlabel", typeBycode==null?"":typeBycode.lable);
                data.put("argname",_m.getArgname());
                data.put("cslx",_m.getCslx());
                data.put("requeriedlabel",_m.getIsrequeried()==1?"是":"否");
                data.put("bz",_m.getBz());
                data.put("tjsql",_m.getTjsql());
               datalist.add(data);
            }
            return Result.ok(datalist);
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
            return Result.error("系统错误，请稍候再试！");

        }

    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request,
                                     HttpServletResponse response)throws Exception{

        if(StringUtil.isEmptyOrLength0(id)){
            return Result.error("参数有误");
        }
        try {

            devtApiTjoptEntityService.delete(id);
            return Result.ok("删除成功");

        }catch (Exception ex){
            ex.printStackTrace();

            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return Result.error("系统错误，请稍候再试！");
        }

    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(HttpServletRequest request,
                                     HttpServletResponse response,
                                     ModelMap model, DevtApiTjoptEntity pFunctiontjoptEntity) throws IOException {

        if(StringUtil.isEmptyOrLength0(pFunctiontjoptEntity.getArgname())){
            return Result.error("参数名称不能为空");
        }
        if(StringUtil.isEmptyOrLength0(pFunctiontjoptEntity.getSubid())){
            return Result.error("功能id不能为空");
        }
        List<DevtApiSubEntity> devtApiSubEntities = devtApiSubEntityService.getOneData(pFunctiontjoptEntity.getSubid());
        if(devtApiSubEntities.get(0).getLx()== DevtApiMainEntityService.FunctionType.DATALIST.getValue()&&StringUtil.isEmptyOrLength0(pFunctiontjoptEntity.getTjsql())){
            return Result.error("条件sql不能为空");
        }

        try {

            if(StringUtil.isNullOrSpace(pFunctiontjoptEntity.getId())){
                pFunctiontjoptEntity.setId(UUID.randomUUID().toString());
            }
            devtApiTjoptEntityService.save(pFunctiontjoptEntity);
            
           return Result.ok("保存成功");

        }catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".save() 方法失败 】异常："+ex.getMessage());
            return Result.error("系统错误，请稍候再试！");
        }

    }


}
